package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasure_back.mapper.GoodsMapper;
import com.example.treasure_back.model.GoodsModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class GoodsService {

    @Resource
    GoodsMapper goodsMapper;

    public Integer getGoodsPriceById(Integer id){
        GoodsModel goodsModel = goodsMapper.selectById(id);
        return goodsModel.getPrice();
    }

    public Integer getGoodsNumberById(Integer id){
        GoodsModel goodsModel = goodsMapper.selectById(id);
        return goodsModel.getNumber();
    }

    public String getGoodsNameById(Integer id){
        GoodsModel goodsModel = goodsMapper.selectById(id);
        return goodsModel.getName();
    }

    public String getGoodsImageById(Integer id){
        GoodsModel goodsModel = goodsMapper.selectById(id);
        return goodsModel.getImageUrl();
    }

    public void reduceGoodsById(Integer id){
        GoodsModel goodsModel = goodsMapper.selectById(id);
        UpdateWrapper<GoodsModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("number",goodsModel.getNumber()-1);
        updateWrapper.eq("id",id);
        goodsMapper.update(goodsModel,updateWrapper);
    }
}
